package com.wxblockchain.prometheum;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.wxblockchain.core.base.BaseActivity;
import com.wxblockchain.prometheum.managers.FlutterFragmentManager;

import java.lang.ref.WeakReference;

import io.flutter.embedding.android.FlutterFragment;

public class MainActivity extends BaseActivity {
    private String mCurrentLabel = "Market";

    private FragmentSwitchTool switchTool;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().hide();

        setContentView(R.layout.activity_main);

        BottomNavigationView navView = findViewById(R.id.nav_view);
//        FrameLayout fLayout = findViewById(R.id.nav_host_fragment);

        switchTool = new FragmentSwitchTool(getSupportFragmentManager(),R.id.nav_host_fragment);

        switchTool.changeTab("Market");

        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (mCurrentLabel.equals(item.getTitle().toString())) return true;

                switchTool.changeTab(item.getTitle().toString());

                mCurrentLabel = item.getTitle().toString();

                return true;
            }
        });
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

        FlutterFragment flutterFragment = getPageFlutterFragment();
        if (null != flutterFragment) flutterFragment.onPostResume();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        FlutterFragment flutterFragment = getPageFlutterFragment();
        if (null != flutterFragment) flutterFragment.onNewIntent(intent);
    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount()>0){
            getFragmentManager().popBackStackImmediate();
        }else{
            super.onBackPressed();
        }
//
//        FlutterFragment flutterFragment = getPageFlutterFragment();
//        if (null != flutterFragment) flutterFragment.onBackPressed();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        FlutterFragment flutterFragment = getPageFlutterFragment();
        if (null != flutterFragment) flutterFragment.onRequestPermissionsResult(
                requestCode,
                permissions,
                grantResults
        );
    }

    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();

        FlutterFragment flutterFragment = getPageFlutterFragment();
        if (null != flutterFragment) flutterFragment.onUserLeaveHint();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);

        FlutterFragment flutterFragment = getPageFlutterFragment();
        if (null != flutterFragment) flutterFragment.onTrimMemory(level);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        FlutterFragmentManager.getInstance().destoryAllFlutterFragment();
        FlutterFragmentManager.getInstance().unRegisterAll();
    }

    private FlutterFragment getPageFlutterFragment() {
        WeakReference<FlutterFragment> weakRef =  FlutterFragmentManager.getInstance().getregisteredFlutterFragment(mCurrentLabel);

        if (weakRef != null) {
            return weakRef.get();
        }

        return null;
    }

}
